<?php 
class Zend_View_Helper_LoggedInUser extends Zend_View_Helper_Abstract
{
	public function loggedInUser(){
		$auth = Zend_Auth::getInstance();
		$result = array();
		if($auth->hasIdentity()){
			$username = $auth->getIdentity()->name;
			$status = $auth->getIdentity()->status;
			$id = $auth->getIdentity()->id;
			$user = new Application_Model_User($id);
			$points = $user->countPoints();
			$logoutUrl = $this->view->url(array('controller' => 'auth', 'action' => 'logout'), 'default', true);
			$result['profile'] = $this->_getUserLatestBets($id);
			$result['loginout'] = 'Welcome ' . $username . '(' . $status . ')(' . $points . ')<br /><a href="' . $logoutUrl . '">Logout</a>';
			return $result;
		}
		else{
			$request = Zend_Controller_Front::getInstance()->getRequest();
			$controller = $request->getControllerName();
			$action = $request->getActionName();
			if($controller == 'auth' && $action == 'login'){
				$result['loginout'] = '';
				return $result;
			}
			else{
				$form = new Application_Form_Login();
				$form->setAction($this->view->url(array('controller' => 'auth', 'action' => 'login'), 'default', true));
				$regUrl = $this->view->url(array('controller' => 'auth', 'action' => 'register'), 'default', true);
				$result['loginout'] = $form . '<p><a href="' . $regUrl . '">Register</a></p>';
				return $result;			
			} 
		}
	}
	protected function _getUserLatestBets($id)
	{
		$user = new Application_Model_User($id);
		$bets = $user->getBets(3);
		$result = array();
		foreach ($bets as $bet){
			$title = $bet->getEvent()->title;
			$bet_score = $bet->bet_left . ' : ' . $bet->bet_right;
			$result[] = $title . ' = ' . '<a href="' . $this->view->url(array('controller' => 'event', 'id' => $bet->event_id), 'def', true) . '">' . $bet_score . '</a><BR />';
		}
		return $result;
	}
}